$(function () {
    $.get("/lession05/getShoppingCart", function (data) {
        console.log(data)
        if (!data.error) {
            var str = "";
            data.data.forEach(value => {
                str += `
                <tr>
                <td>
                    <input data-id="${value.goods_id}" type="checkbox">
                </td>
                <td title="${value.goods_name}">
                    ${value.goods_name}
                </td>
                <td>
                    <img src="${value.goods_small_logo}"
                        alt="">
                </td>
                <td>
                    ${value.goods_price}
                </td>
                <td  class="add" id="${value.goods_id}">
                     +
                </td>
                <td>
                   ${value.num}
                </td>
                <td class="less"  id="${value.goods_id}">
                -
                 </td>
                <td class="remove" id="${value.goods_id}">
                    x
                 </td>
                
                `
            })



            $(str).appendTo("tbody")

            let shoppingCart = data.data;
            //全选按钮
            let $allSelect = $("thead input")
            //tbody中的所有按钮
            let $goodsInput = $("tbody input")
            //监听全选按钮的改变
            $allSelect.change(function () {
                let isChecked = $(this).prop('checked');

                $goodsInput.prop('checked', isChecked);

                shoppingCart.forEach(value => value.checked = isChecked)
                var allPrice = computePrice()

            })
            //单个商品的取消


            $goodsInput.change(function () {
                var isAllChecked = true

                $goodsInput.each(function (index, value) {
                    if (!$(value).is(":checked")) {
                        isAllChecked = false
                    }
                })

                $allSelect.prop("checked", isAllChecked)

                var id = $(this).attr("data-id")
                console.log(id)




                var obj = shoppingCart.find(value => value.goods_id === id)

                obj.checked = !obj.checked

                computePrice()
            })

            //增加
            $("tbody").on("click", ".add", function () {



                let id = $(this).attr("id")


                $.get("/lession05/addToCart", { id }, (data) => {
                    if (!data.error) {

                        //获取页面中商品的数量
                        var nowNum = $(this).next().html()

                        //改变页面中商品的数量
                        $(this).next().html(+nowNum + 1)

                        let obj = shoppingCart.find(value => value.goods_id == id)


                        obj.num++
                        computePrice()
                    }
                })

            })

            //减少
            $("tbody").on("click", ".less", function () {
                // 获取商品id
                let id = $(this).attr('id');
                let obj = shoppingCart.find(value => value.goods_id == id);
                if (obj.num > 1) {
                    $.get('/lession05/minusCart', { id }, (data) => {
                        if (!data.error) {

                            // 减少数量成功
                            var nowNum = $(this).prev().html();  // 获取现
                            $(this).prev().html(+nowNum - 1);// 改页面


                            // 改变数组中的数量

                            obj.num--
                            // 计算价格
                            computePrice();
                        }
                    })
                }
            })



            //删除
            $("tbody").on("click", ".remove", function () {
                let id = $(this).attr("id");

                $.get("/lession05/deleteCart", { id }, (data) => {

                    $(this).parents("tr").remove()
                    let idx = shoppingCart.findIndex(value => value.goods_id == id);


                    shoppingCart.splice(idx, 1);
                    computePrice()
                })
            })



            //计算

            function computePrice() {
                var checkShoppingCart = shoppingCart.filter(value => value.checked)

                var allPrice = checkShoppingCart.reduce((prev, value) => prev + value.goods_price * value.num, 0)

                $("#allCount").html(allPrice)
            }
        }
    })
})